﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using Excel = Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;

namespace MSL.Utility.Doc
{
    /// <summary>
    /// Excel操作类
    /// </summary>
    public class Xls
    {
        #region 构造/析构函数
        /// <summary>
        /// 
        /// </summary>
        public Xls()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        ~Xls()
        {
            Dispose();
        }
        #endregion

        #region 导出至Excel文件
        /// </summary> 
        /// 导出Excel文件，自动返回可下载的文件流 
        /// </summary> 
        public void DataTable1Excel(DataTable dataTable)
        {
            GridView gvExport = null;
            HttpContext curContext = HttpContext.Current;
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;
            if (dataTable != null)
            {
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                curContext.Response.Charset = "utf-8";
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                gvExport = new GridView();
                gvExport.DataSource = dataTable.DefaultView;
                gvExport.AllowPaging = false;
                gvExport.DataBind();
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"/>" + strWriter.ToString());
                curContext.Response.End();
            }
        }

        /// <summary>
        /// 导出Excel文件，转换为可读模式
        /// </summary>
        public void DataTable2Excel(DataTable dataTable)
        {
            DataGrid dgExport = null;
            HttpContext curContext = HttpContext.Current;
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;

            if (dataTable != null)
            {
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                curContext.Response.Charset = "";
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                dgExport = new DataGrid();
                dgExport.DataSource = dataTable.DefaultView;
                dgExport.AllowPaging = false;
                dgExport.DataBind();
                dgExport.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
        }

        /// <summary>
        /// 导出Excel文件，并自定义文件名
        /// </summary>
        public void DataTable3Excel(DataTable dataTable, String fileName)
        {
            GridView dgExport = null;
            HttpContext curContext = HttpContext.Current;
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;

            if (dataTable != null)
            {
                HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
                curContext.Response.ContentType = "application nd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                curContext.Response.Charset = "GB2312";
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                dgExport = new GridView();
                dgExport.DataSource = dataTable.DefaultView;
                dgExport.AllowPaging = false;
                dgExport.DataBind();
                dgExport.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
        }

        #endregion

        #region 获得读取Excel文件的连接
        /// <summary>
        /// 
        /// </summary>
        /// <param name="excelFilePath"></param>
        /// <returns></returns>
        private String GetExcelConnString(string excelFilePath)
        {
            string strConn = "";
            if (excelFilePath.EndsWith(".xlsx"))
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1;'";
            }
            else
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'";
            }
            return strConn;
        }

        #endregion

        #region 获取Excel文件数据表列表

        /// <summary>
        /// 获取Excel文件数据表列表
        /// </summary>
        public ArrayList GetExcelTables(string excelFileName)
        {
            DataTable dt = new DataTable();
            ArrayList TablesList = new ArrayList();
            if (File.Exists(excelFileName))
            {
                using (OleDbConnection conn = new OleDbConnection(GetExcelConnString(excelFileName)))
                {
                    try
                    {
                        conn.Open();
                        dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                    }
                    catch (Exception exp)
                    {
                        throw exp;
                    }

                    //获取数据表个数
                    int tablecount = dt.Rows.Count;
                    for (int i = 0; i < tablecount; i++)
                    {
                        string tablename = dt.Rows[i][2].ToString().Trim();//.TrimEnd('$');
                        if (TablesList.IndexOf(tablename) < 0)
                        {
                            TablesList.Add(tablename);
                        }
                    }
                }
            }
            return TablesList;
        }
        #endregion

        #region 获取Excel文件指定数据表的数据列表

        /// <summary>
        /// 获取Excel文件指定数据表的数据列表
        /// </summary>
        /// <param name="excelFileName">Excel文件名</param>
        /// <param name="tableName">数据表名</param>
        public ArrayList GetExcelTableColumns(string excelFileName, string tableName)
        {
            DataTable dt = new DataTable();
            ArrayList ColsList = new ArrayList();
            if (File.Exists(excelFileName))
            {
                using (OleDbConnection conn = new OleDbConnection(GetExcelConnString(excelFileName)))
                {
                    conn.Open();
                    dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName, null });
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string colname = dt.Rows[i]["Column_Name"].ToString().Trim();
                        ColsList.Add(colname);
                    }
                }
            }
            return ColsList;
        }
        #endregion

        #region 导入

        /// <summary>
        /// 将Excel文件导出至DataTable(第一行作为表头)
        /// </summary>
        /// <param name="filePath">Excel文件路径</param>
        /// <param name="tableName">数据表名，如果数据表名错误，默认为第一个数据表名</param>
        public DataTable ImportExcel(string filePath, string tableName)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("Excel文件不存在！");
            }

            //如果数据表名不存在，则数据表名为Excel文件的第一个数据表
            ArrayList TableList = new ArrayList();
            TableList = GetExcelTables(filePath);

            if (tableName.IndexOf(tableName) < 0)
            {
                tableName = TableList[0].ToString().Trim();
            }

            DataTable table = new DataTable();
            OleDbConnection dbcon = new OleDbConnection(GetExcelConnString(filePath));
            OleDbCommand cmd = new OleDbCommand("select * from [" + tableName + "$]", dbcon);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

            try
            {
                if (dbcon.State == ConnectionState.Closed)
                {
                    dbcon.Open();
                }
                adapter.Fill(table);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                if (dbcon.State == ConnectionState.Open)
                {
                    dbcon.Close();
                }
            }
            return table;
        }
        /// <summary>
        /// 把Excel的某个工作表导入到DataTable中
        /// </summary>
        /// <param name="fileName">文件完整路径</param>
        /// <param name="sheetName">工作表名</param>
        /// <param name="HasHeader">是否存在表头。若是会把工作表第一行转成DataTable的列名</param>
        /// <returns>导入后的DataTable</returns>
        public DataTable ImportExcel(string fileName, string sheetName, bool HasHeader)
        {
            DataTable table = ExecuteSql(fileName, sheetName);// ExecuteReader(fileName, "select * from [" + sheetName + "]", null);

            if (HasHeader)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                    table.Columns[i].ColumnName = table.Rows[0][i].ToString();
                table.Rows.RemoveAt(0);
            }

            return table;
        }

        /// <summary>
        /// 把Excel的Sheet工作表导入到DataTable中
        /// </summary>
        /// <param name="fileName">文件完整路径</param>
        /// <param name="HasHeader">是否存在表头。若是会把工作表第一行转成DataTable的列名</param>
        /// <returns>导入后的DataTable</returns>
        public DataTable ImportExcel(string fileName, bool HasHeader)
        {
            return ImportExcel(fileName, "sheet1$", HasHeader);
        }

        /// <summary>
        /// 把Excel的某个工作表导入到DataTable中
        /// </summary>
        /// <param name="fileName">文件完整路径</param>
        /// <returns></returns>
        public DataTable ImportExcel(string fileName)
        {
            return ImportExcel(fileName, true);
        }

        #endregion

        #region 导出

        /// <summary>
        /// 将数据导出至Excel文件
        /// </summary>
        /// <param name="dataTable">DataTable对象</param>
        /// <param name="columns">要导出的数据列集合</param>
        /// <param name="filePath">Excel文件路径</param>
        public bool ExportExcel(DataTable dataTable, ArrayList columns, string filePath)
        {
            if (File.Exists(filePath))
            {
                throw new Exception("该文件已经存在！");
            }

            //如果数据列数大于表的列数，取数据表的所有列
            if (columns.Count > dataTable.Columns.Count)
            {
                for (int s = dataTable.Columns.Count + 1; s <= columns.Count; s++)
                {
                    columns.RemoveAt(s);   //移除数据表列数后的所有列
                }
            }

            //遍历所有的数据列，如果有数据列的数据类型不是 DataColumn，则将它移除
            DataColumn column = new DataColumn();
            for (int j = 0; j < columns.Count; j++)
            {
                try
                {
                    column = (DataColumn)columns[j];
                }
                catch (Exception)
                {
                    columns.RemoveAt(j);
                }
            }
            if ((dataTable.TableName.Trim().Length == 0) || (dataTable.TableName.ToLower() == "table"))
            {
                dataTable.TableName = "Sheet1";
            }

            //数据表的列数
            int ColCount = columns.Count;

            //创建参数
            OleDbParameter[] para = new OleDbParameter[ColCount];

            //创建表结构的SQL语句
            string TableStructStr = @"Create dataTable " + dataTable.TableName + "(";

            //连接字符串
            string connString = GetExcelConnString(filePath);
            OleDbConnection objConn = new OleDbConnection(connString);

            //创建表结构
            OleDbCommand objCmd = new OleDbCommand();

            //数据类型集合
            ArrayList DataTypeList = new ArrayList();
            DataTypeList.Add("System.Decimal");
            DataTypeList.Add("System.Double");
            DataTypeList.Add("System.Int16");
            DataTypeList.Add("System.Int32");
            DataTypeList.Add("System.Int64");
            DataTypeList.Add("System.Single");

            DataColumn col = new DataColumn();

            //遍历数据表的所有列，用于创建表结构
            for (int k = 0; k < ColCount; k++)
            {
                col = (DataColumn)columns[k];

                //列的数据类型是数字型
                if (DataTypeList.IndexOf(col.DataType.ToString().Trim()) >= 0)
                {
                    para[k] = new OleDbParameter("@" + col.Caption.Trim(), OleDbType.Double);
                    objCmd.Parameters.Add(para[k]);

                    //如果是最后一列
                    if (k + 1 == ColCount)
                    {
                        TableStructStr += col.Caption.Trim() + " Double)";
                    }
                    else
                    {
                        TableStructStr += col.Caption.Trim() + " Double,";
                    }
                }
                else
                {
                    para[k] = new OleDbParameter("@" + col.Caption.Trim(), OleDbType.VarChar);
                    objCmd.Parameters.Add(para[k]);

                    //如果是最后一列
                    if (k + 1 == ColCount)
                    {
                        TableStructStr += col.Caption.Trim() + " VarChar)";
                    }
                    else
                    {
                        TableStructStr += col.Caption.Trim() + " VarChar,";
                    }
                }
            }

            //创建Excel文件及文件结构
            try
            {
                objCmd.Connection = objConn;
                objCmd.CommandText = TableStructStr;

                if (objConn.State == ConnectionState.Closed)
                {
                    objConn.Open();
                }
                objCmd.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                throw exp;
            }

            //插入记录的SQL语句
            string InsertSql_1 = "Insert into " + dataTable.TableName + " (";
            string InsertSql_2 = " Values (";
            string InsertSql = "";

            //遍历所有列，用于插入记录，在此创建插入记录的SQL语句
            for (int colID = 0; colID < ColCount; colID++)
            {
                if (colID + 1 == ColCount)  //最后一列
                {
                    InsertSql_1 += columns[colID].ToString().Trim() + ")";
                    InsertSql_2 += "@" + columns[colID].ToString().Trim() + ")";
                }
                else
                {
                    InsertSql_1 += columns[colID].ToString().Trim() + ",";
                    InsertSql_2 += "@" + columns[colID].ToString().Trim() + ",";
                }
            }

            InsertSql = InsertSql_1 + InsertSql_2;

            //遍历数据表的所有数据行
            DataColumn DataCol = new DataColumn();
            for (int rowID = 0; rowID < dataTable.Rows.Count; rowID++)
            {
                for (int colID = 0; colID < ColCount; colID++)
                {
                    //因为列不连续，所以在取得单元格时不能用行列编号，列需得用列的名称
                    DataCol = (DataColumn)columns[colID];
                    if (para[colID].DbType == DbType.Double && dataTable.Rows[rowID][DataCol.Caption].ToString().Trim() == "")
                    {
                        para[colID].Value = 0;
                    }
                    else
                    {
                        para[colID].Value = dataTable.Rows[rowID][DataCol.Caption].ToString().Trim();
                    }
                }
                try
                {
                    objCmd.CommandText = InsertSql;
                    objCmd.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    string str = exp.Message;
                }
            }
            try
            {
                if (objConn.State == ConnectionState.Open)
                {
                    objConn.Close();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return true;
        }
        /// <summary>
        /// 把DataTable导出到一个Excel文件，并增加表头
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <param name="table">要导出的DataTable</param>
        public void ExportExcel(string fileName, DataTable table)
        {
            ExportExcel(fileName, table, true);
        }

        /// <summary>
        /// 把DataTable导出到一个Excel文件
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <param name="table">要导出的DataTable</param>
        /// <param name="AddHeader">是否要增加表头</param>
        public void ExportExcel(string fileName, DataTable table, bool AddHeader)
        {
            object missing = System.Reflection.Missing.Value;
            Excel.Application app = new Excel.Application();
            app.Application.Workbooks.Add(true);
            Excel.Workbook book = (Excel.Workbook)app.ActiveWorkbook;
            Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;

            if (AddHeader)
                for (int i = 0; i < table.Columns.Count; i++)
                    sheet.Cells[1, i + 1] = table.Columns[i].ColumnName;

            for (int i = 0, ei = AddHeader ? 2 : 1; i < table.Rows.Count; i++, ei++)
                for (int j = 0; j < table.Columns.Count; j++)
                    sheet.Cells[ei, j + 1] = table.Rows[i][j];


            string path = fileName.Substring(0, fileName.LastIndexOf('\\') + 1).Trim('\\');
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            book.SaveCopyAs(fileName);
            //关闭文件
            book.Close(false, missing, missing);
            //退出excel
            app.Quit();
        }

        /// <summary>
        /// 将数据导出至Excel文件
        /// </summary>
        /// <param name="Table">DataTable对象</param>
        /// <param name="filePath">Excel文件路径</param>
        public bool ExportExcel(DataTable Table, string filePath)
        {
            if (File.Exists(filePath))
            {
                throw new Exception("该文件已经存在！");
            }

            if ((Table.TableName.Trim().Length == 0) || (Table.TableName.ToLower() == "table"))
            {
                Table.TableName = "Sheet1";
            }

            //数据表的列数
            int ColCount = Table.Columns.Count;

            //用于记数，实例化参数时的序号
            int i = 0;

            //创建参数
            OleDbParameter[] para = new OleDbParameter[ColCount];

            //创建表结构的SQL语句
            string TableStructStr = @"Create Table " + Table.TableName + "(";

            //连接字符串
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;";
            OleDbConnection objConn = new OleDbConnection(connString);

            //创建表结构
            OleDbCommand objCmd = new OleDbCommand();

            //数据类型集合
            ArrayList DataTypeList = new ArrayList();
            DataTypeList.Add("System.Decimal");
            DataTypeList.Add("System.Double");
            DataTypeList.Add("System.Int16");
            DataTypeList.Add("System.Int32");
            DataTypeList.Add("System.Int64");
            DataTypeList.Add("System.Single");

            //遍历数据表的所有列，用于创建表结构
            foreach (DataColumn col in Table.Columns)
            {
                //如果列属于数字列，则设置该列的数据类型为double
                if (DataTypeList.IndexOf(col.DataType.ToString()) >= 0)
                {
                    para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.Double);
                    objCmd.Parameters.Add(para[i]);

                    //如果是最后一列
                    if (i + 1 == ColCount)
                    {
                        TableStructStr += col.ColumnName + " double)";
                    }
                    else
                    {
                        TableStructStr += col.ColumnName + " double,";
                    }
                }
                else
                {
                    para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.VarChar);
                    objCmd.Parameters.Add(para[i]);

                    //如果是最后一列
                    if (i + 1 == ColCount)
                    {
                        TableStructStr += col.ColumnName + " varchar)";
                    }
                    else
                    {
                        TableStructStr += col.ColumnName + " varchar,";
                    }
                }
                i++;
            }

            //创建Excel文件及文件结构
            try
            {
                objCmd.Connection = objConn;
                objCmd.CommandText = TableStructStr;

                if (objConn.State == ConnectionState.Closed)
                {
                    objConn.Open();
                }
                objCmd.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                throw exp;
            }

            //插入记录的SQL语句
            string InsertSql_1 = "Insert into " + Table.TableName + " (";
            string InsertSql_2 = " Values (";
            string InsertSql = "";

            //遍历所有列，用于插入记录，在此创建插入记录的SQL语句
            for (int colID = 0; colID < ColCount; colID++)
            {
                if (colID + 1 == ColCount)  //最后一列
                {
                    InsertSql_1 += Table.Columns[colID].ColumnName + ")";
                    InsertSql_2 += "@" + Table.Columns[colID].ColumnName + ")";
                }
                else
                {
                    InsertSql_1 += Table.Columns[colID].ColumnName + ",";
                    InsertSql_2 += "@" + Table.Columns[colID].ColumnName + ",";
                }
            }

            InsertSql = InsertSql_1 + InsertSql_2;

            //遍历数据表的所有数据行
            for (int rowID = 0; rowID < Table.Rows.Count; rowID++)
            {
                for (int colID = 0; colID < ColCount; colID++)
                {
                    if (para[colID].DbType == DbType.Double && Table.Rows[rowID][colID].ToString().Trim() == "")
                    {
                        para[colID].Value = 0;
                    }
                    else
                    {
                        para[colID].Value = Table.Rows[rowID][colID].ToString().Trim();
                    }
                }
                try
                {
                    objCmd.CommandText = InsertSql;
                    objCmd.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    string str = exp.Message;
                }
            }
            try
            {
                if (objConn.State == ConnectionState.Open)
                {
                    objConn.Close();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return true;
        }

        #endregion

        #region 读取

        /// <summary>
        /// 执行SQL查询一个Excel文档的内容 
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <param name="sheetName">sheet名</param>
        /// <returns>查询结果</returns>
        public DataTable ExecuteSql(string fileName, string sheetName)
        {
            string strCon = GetExcelConnString(fileName);
            OleDbConnection cnnxls = null;
            OleDbDataAdapter adapter = null;
            DataTable table = new DataTable();
            try
            {
                cnnxls = new OleDbConnection(strCon);
                string cmdText = "select * from [" + sheetName + "]";

                adapter = new OleDbDataAdapter(cmdText, cnnxls);
                adapter.Fill(table);
            }
            finally
            {
                if (cnnxls.State == ConnectionState.Open)
                {
                    cnnxls.Close();
                }
            }
            return table;
        }

        #endregion

        #region 编辑

        /// <summary>
        /// 编辑一个Excel文档
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <param name="table">要编辑的内容</param>
        /// <param name="startCell">目标单元格位置</param>
        public void EditExcel(string fileName, DataTable table, string startCell)
        {
            Tuple<int, int> cell = ConvertCell(startCell);

            object missing = System.Reflection.Missing.Value;
            Excel.Application app = new Excel.Application();
            app.Application.Workbooks.Add(true);

            Excel.Workbook book = null;
            Excel.Worksheet sheet = null;
            bool existFile = IsExistFile(fileName);
            if (existFile)
            {
                book = app.Workbooks.Open(fileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
            }
            else
            {
                book = (Excel.Workbook)app.ActiveWorkbook;
            }
            sheet = (Excel.Worksheet)book.ActiveSheet;

            for (int i = 0, ei = cell.Item1; i < table.Rows.Count; i++, ei++)
                for (int j = 0, ej = cell.Item2; j < table.Columns.Count; j++, ej++)
                    sheet.Cells[ei, ej] = table.Rows[i][j];


            if (existFile) book.Save();
            else book.SaveCopyAs(fileName);
            //关闭文件
            book.Close(false, missing, missing);
            //退出excel
            app.Quit();

        }

        /// <summary>
        /// 编辑一个Excel文档
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <param name="table">要编辑的内容</param>
        public void EditExcel(string fileName, DataTable table)
        {
            EditExcel(fileName, table, "A1");
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 检查文件是否存在，若不存在则会先确保文件所在的目录存在
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>检查结果</returns>
        private bool IsExistFile(string fileName)
        {
            if (File.Exists(fileName)) return true;
            string path = fileName.Substring(0, fileName.LastIndexOf('\\') + 1).Trim('\\');
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return false;
        }

        /// <summary>
        /// 转换单元格位置
        /// </summary>
        /// <param name="cell">单元格位置</param>
        /// <returns>int二元组</returns>
        private Tuple<int, int> ConvertCell(string cell)
        {
            Match colM = Regex.Match(cell, @"[a-zA-Z]+");
            if (String.IsNullOrEmpty(colM.Value))
                throw new Exception("单元格格式有误！");
            string colStr = colM.Value.ToUpper();
            int colIndex = 0;
            foreach (char ci in colStr)
                colIndex += 1 + (ci - 'A');

            Match rowM = Regex.Match(cell, @"\d+");
            if (String.IsNullOrEmpty(rowM.Value))
                throw new Exception("单元格格式有误！");
            int rowIndex = System.Convert.ToInt32(rowM.Value);

            Tuple<int, int> result = new Tuple<int, int>(rowIndex, colIndex);
            return result;
        }

        private void Dispose()
        {
            //Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}